package com.chu.controller;

import com.chu.entity.Dept;
import com.chu.entity.ResumeManage;
import com.chu.entity.Role;
import com.chu.enums.PageEnum;
import com.chu.service.DeptService;
import com.chu.service.ResumeManageService;
import com.chu.service.RoleService;
import com.chu.utils.ResultVO;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
public class ResumeManageController {

    @Autowired
    private ResumeManageService service;

    @Autowired
    private DeptService deptService;

    @Autowired
    private RoleService roleService;

    //显示所有简历模糊查
    @RequestMapping("/zhaopin/demo2/checkResume")
    @RequiresPermissions("user:checkResume")
    public String checkResume(@RequestParam(defaultValue = "1")int page,
                              String username, String roleName, String workExp,
                              String registerDate, ModelMap map){
        PageInfo<ResumeManage> pageInfo = service.findAllResumeManage(page, PageEnum.SIZE.getSize(),username,roleName,workExp,registerDate);
        map.addAttribute("pageInfo",pageInfo);
        map.addAttribute("username",username);
        map.addAttribute("roleName",roleName);
        map.addAttribute("workExp",workExp);
        map.addAttribute("registerDate",registerDate);
        return "/zhaopin/demo2/list";
    }
    //删除简历
    @RequestMapping("/zhaopin/demo2/deleteResume")
    public void deleteResume(int id, HttpServletResponse response){
        response.setContentType("text/html;charset=UTF-8");
        ObjectMapper mapper = new ObjectMapper();
        int result = service.deleteByPrimaryKey(id);
        if(result>0){
            try {
                response.getWriter().write(mapper.writeValueAsString(new ResultVO(0,"删除成功！")));
            } catch (IOException e) {
                e.printStackTrace();
            }

        }else{
            try {
                response.getWriter().write(mapper.writeValueAsString(new ResultVO(13,"删除失败！")));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    //增加简历的职位回显
    @RequestMapping("/zhaopin/demo2/checkDept")
    public String checkDept(ModelMap map){

        List<Role> roleList = roleService.selectAllRole();
        map.addAttribute("roleList",roleList);
        return "/zhaopin/demo2/add";

    }
    //增加简历的封装
    @RequestMapping("/zhaopin/demo2/saveResume")
    public String saveResume(ResumeManage resumeManage,HttpServletResponse response){
        response.setContentType("text/html;charset=UTF-8");
        int result = service.insertSelective(resumeManage);
        if(result>0){
            try {
                response.getWriter().write("<script>alert('增加成功！');location.href='/zhaopin/demo2/checkResume'</script>");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else{
            try {
                response.getWriter().write("<script>alert('增加失败！');location.href='/zhaopin/demo2/checkDept'</script>");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;

    }

    //修改简历回显
    @RequestMapping("/zhaopin/demo2/checkOneResume")
    public String checkOneResume(int id,ModelMap map){

        ResumeManage manage = service.selectByPrimaryKey(id);
        map.addAttribute("manage",manage);
        List<Role> roleList = roleService.selectAllRole();
        map.addAttribute("roleList",roleList);
        return "/zhaopin/demo2/update";

    }

    //修改简历的封装
    @RequestMapping("/zhaopin/demo2/updateResume")
    public String updateResume(ResumeManage resumeManage,HttpServletResponse response){
        response.setContentType("text/html;charset=UTF-8");
        int result = service.updateByPrimaryKeySelective(resumeManage);
        if(result>0){
            try {
                response.getWriter().write("<script>alert('修改成功！');location.href='/zhaopin/demo2/checkResume'</script>");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else{
            try {
                response.getWriter().write("<script>alert('修改失败！');location.href='/zhaopin/demo2/checkOneResume'</script>");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;

    }







}
